package com.rw.coco.event;

import com.rw.coco.api.event.GroupMessageEvent;
import com.rw.coco.api.modle.Account;
import com.rw.coco.api.modle.Bot;
import com.rw.coco.api.modle.Group;
import com.rw.coco.model.MiraiAccount;
import com.rw.coco.model.MiraiBot;
import com.rw.coco.model.MiraiGroup;
import net.mamoe.mirai.contact.Stranger;

public class MiraiGroupMessageEvent implements GroupMessageEvent {
    private final net.mamoe.mirai.event.events.GroupMessageEvent groupMessageEvent;

    public MiraiGroupMessageEvent(net.mamoe.mirai.event.events.GroupMessageEvent groupMessageEvent) {
        this.groupMessageEvent = groupMessageEvent;
    }

    @Override
    public boolean isAdmin() {
        return groupMessageEvent.getPermission().getLevel() >= 1;
    }

    @Override
    public Group getGroup() {
        String id = String.valueOf(groupMessageEvent.getGroup().getId());
        String name = groupMessageEvent.getGroup().getName();
        return new MiraiGroup(id, name);
    }

    @Override
    public void quit() {
        groupMessageEvent.getGroup().quit();
    }

    @Override
    public Bot getBot() {
        net.mamoe.mirai.Bot bot = groupMessageEvent.getBot();
        return new MiraiBot(String.valueOf(bot.getId()), bot.getNick());
    }

    @Override
    public String getMessage() {
        return groupMessageEvent.getMessage().contentToString();
    }

    @Override
    public Account getSender() {
        String id = String.valueOf(groupMessageEvent.getSender().getId());
        String nameCard = groupMessageEvent.getSender().getNameCard();
        String nick = groupMessageEvent.getSender().getNick();
        return new MiraiAccount(id, nameCard, nick);
    }

    @Override
    public void sendBack(String message) {
        groupMessageEvent.getSubject().sendMessage(message);
    }

    @Override
    public void sendBackSender(String message) {
        groupMessageEvent.getSender().sendMessage(message);
    }

    @Override
    public void sendPrivate(String message, String accountId) {
        Stranger stranger = groupMessageEvent.getBot().getStranger(Long.parseLong(accountId));
        if (stranger != null) {
            stranger.sendMessage(message);
        }
    }
}
